package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"role-mgr1/cache"
	"role-mgr1/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carName := c.PostForm("carName")
	carCode := c.PostForm("carCode")
	carNo := c.PostForm("carNo")
	if carNo == "" || carCode == "" || carName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Car{
		CarName: carName,
		CarCode: carCode,
		CarNo:   carNo,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	Cid, _ := strconv.Atoi(c.PostForm("cid"))
	orderCode := c.PostForm("orderCode")
	orderForm := c.PostForm("orderForm")
	orderTo := c.PostForm("orderTo")
	if Cid == 0 || orderTo == "" || orderCode == "" || orderForm == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Order{
		OrderCode: orderCode,
		OrderForm: orderForm,
		OrderTo:   orderTo,
		Cid:       Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var orderList []models.CarOrder
	/*
		SELECT
		  orders.id AS OrderId,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_no AS CarNo,
		  orders.order_form AS OrderForm,
		  orders.order_code AS OrderCode,
		  orders.order_to AS OrderTo
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").Select(
			"orders.id AS OrderId",
			"cars.car_code AS CarCode",
			"cars.car_name AS CarName",
			"cars.car_no AS CarNo",
			"orders.order_form AS OrderForm",
			"orders.order_code AS OrderCode",
			"orders.order_to AS OrderTo",
		).Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Offset(offset).
			Limit(size).Debug().
			Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  orderList,
		"count": count,
	})
	return
}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var orderList models.CarOrder
	/*
		SELECT
		  orders.id AS OrderId,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_no AS CarNo,
		  orders.order_form AS OrderForm,
		  orders.order_code AS OrderCode,
		  orders.order_to AS OrderTo
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").Select(
			"orders.id AS OrderId",
			"cars.car_code AS CarCode",
			"cars.car_name AS CarName",
			"cars.car_no AS CarNo",
			"orders.order_form AS OrderForm",
			"orders.order_code AS OrderCode",
			"orders.order_to AS OrderTo",
		).Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Where("orders.id =?", id).
			Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": orderList,
	})
	return
}
